<template>
    <div>
        <el-form :model="store" :rules="rules" ref="store" label-width="100px" class="demo-ruleForm">
            <el-form-item label="标题" prop="title">
                <el-input v-model="store.title"></el-input>
            </el-form-item>
            <el-form-item label="简介" prop="intro">
                <el-input type="textarea" v-model="store.intro"></el-input>
            </el-form-item>
            <el-form-item label="" prop="status">
                <el-select v-model="store.status" placeholder="是否可见">
                <el-option label="互联网可见" value="是"></el-option>
                <el-option label="仅自己可见" value="否"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="create('store')">确定</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
import $api from '../../api/index'
export default {
    created(){
        let editStore = JSON.parse(localStorage.getItem('editStore'));

        let {title,intro,status,id} = editStore;

        this.store = {
            title,
            intro,
            status,
            id
        }
    },
    data(){
        return {
            store:{
                title:'',
                intro:'',
                status:'',
                id:''
            },
            rules:{
                title: [
                    { required: true, message: '请输入知识库标题', trigger: 'blur' },
                ],
                intro: [
                    { required: true, message: '请输入简介', trigger: 'blur' },
                ],
                status: [
                    { required: true, message: '请选择活动区域', trigger: 'change' }
                ]
            }
        }
    },
    methods: {
        create(formName){
            //创建 id=''  更新 id
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    let {title,intro,status,id} = this.store;
                    let fun = id ? 'update':'create';
                    $api[fun]({title,intro,status},id).then(res => {
                        if(res.code === 1){
                            this.$message({
                                message: id ?'更新知识库成功': '创建知识库成功',
                                type: 'success'
                            });
                            this.store = {
                                title:'',
                                intro:'',
                                status:'',
                                id:''
                            }
                            localStorage.removeItem('editStore');
                            this.$router.push('/main/home/store');
                        }else{
                            this.$message.error(id ?'更新知识库失败': '创建知识库失败');
                        }
                    })
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        }
    },
}
</script>